﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class Templates_ProductItem_Default : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsParent.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsParent_SelectData);
        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);
        //dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlFeed_ItemDataBound);
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);


        dlProductNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlProductNavSiblings_ItemDataBound);
        dlNavParent.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavParent_ItemDataBound);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();




        }
    }

    protected string GetFlash()
    {
        var heroFlash = AcmsContext.CurrentPost.FindPostControl("HeroFlash");
        var host = HttpContext.Current.Request.Url.Host;
        if (heroFlash != null)
        {
            var heroFlashProperties = heroFlash.GetProperties<Acms.AcmsControls.FlashContentControlProperties>();
            if (heroFlashProperties != null)
            {
                return "http://" + host + ResolveUrl(Acms.AcmsControls.RenderFile.GetUrl(heroFlashProperties.ResourceId, null, null, this));
            }
        }
        return null;
    }


    public override void DataBind()
    {
        base.DataBind();
        var post = AcmsContext.CurrentPost;

        if (post != null)
        {
            var pathParts = post.Path.Split('/');
            if (pathParts[1] == "Products")
            {
                phParentNav.Visible = true;
                phSiblingNav.Visible = false;
            }
            else
            {
                phParentNav.Visible = false;
                phSiblingNav.Visible = true;
                phProductSiblingNav.Visible = false;
            }
                //if (pathParts.Length > 1 && pathParts[1] == "Data-Centre-Management")
                //    SectionNav_Control.CssClass="selected";


         
            //  Set Hero Image
            if (pnlItem != null)
            {
                var HeroImage = post.FindPostControl("HeroImage");// as ImageContentControlDisplay;
                if (HeroImage != null)
                {
                    var prop = HeroImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    if (prop != null && prop.HasResource())
                    {
                        pnlItem.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                    }
                }
            }
            
            SetPopup(post, phPopup1, lnk1, acmsTitle1, acmsCopy1, 1);
            SetPopup(post, phPopup2, lnk2, acmsTitle2, acmsCopy2, 2);
            SetPopup(post, phPopup3, lnk3, acmsTitle3, acmsCopy3, 3);
            SetPopup(post, phPopup4, lnk4, acmsTitle4, acmsCopy4, 4);
            SetPopup(post, phPopup5, lnk5, acmsTitle5, acmsCopy5, 5);

            var tabs = post.Children.Where(x => x.TemplateName == "TabHolder").ToArray();
            
            
            //var moreInfo = this.FindControl("moreInfo") as HyperLink;

            if (lnkMoreInfo != null)
            {
                if (tabs.Length > 0)
                {
                    lnkMoreInfo.Visible = true;
                }
                else {
                    lnkMoreInfo.Visible = false;
                }

            }
            rptTabs.DataSource = tabs;
            rptTabs.DataBind();
        }
    } 
    
    private void SetPopup(Post post, PlaceHolder phPopup, HyperLink lnk, TextContentControlDisplay acmsTitle, HtmlContentControlDisplay acmsCopy, int i)
    {
        if (post == null)
            throw new ArgumentNullException("post", string.Format("No post for index {0}", i));
        if (phPopup == null)
            throw new ArgumentNullException("phPopup", string.Format("No phPopup for index {0}", i));
        if (lnk == null)
            throw new ArgumentNullException("lnk", string.Format("No lnk for index {0}", i));
        if (acmsTitle == null)
            throw new ArgumentNullException("acmsTitle", string.Format("No acmsTitle for index {0}", i));
        if (acmsCopy == null)
            throw new ArgumentNullException("acmsCopy", string.Format("No acmsCopy for index {0}", i));

        phPopup.Visible = true;
        
        if (acmsTitle.IsEmpty)
        {
            phPopup.Visible = false;
        }
        else
        {
            lnk.Text = acmsTitle.Text;
        }
        
        if (acmsCopy.IsEmpty)
            phPopup.Visible = false;

        var x = (string)null;
        var posX = post.FindPostControl("Pop" + i + "_X");
        if (posX != null)
        {
            var posXProperty = posX.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
            if (posXProperty != null)
            {
                x = posXProperty.Text.Trim();
            }
        }

        var y = (string)null;
        var posY = post.FindPostControl("Pop" + i + "_Y");
        if (posY != null)
        {
            var posYProperty = posY.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
            if (posYProperty != null)
            {
                y = posYProperty.Text.Trim();
            }
        }

        if (!string.IsNullOrEmpty(x) && !string.IsNullOrEmpty(y))
        {
            lnk.Style.Add("left", x + "px");
            lnk.Style.Add("top", y + "px");
        }
        else
        {
            phPopup.Visible = false;
        }
    }

    void dsParent_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dsTabChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Children.Where(x => x.TemplateName == "TabItem").OrderBy(x => x.Sequence).ToList());
    }


    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }

    void dlProductNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkProductNavSibling") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }

    void dlNavParent_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }
}